package com.sgb.kjwl.view.widget;

import android.content.Context;

import androidx.recyclerview.widget.GridLayoutManager;

import android.util.SparseBooleanArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.request.MaterialFilterJson;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.view.ui.adapter.material.MaterialFilterAdapter3;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;

import java.util.ArrayList;
import java.util.List;

public class MaterialFliterPopWindow extends BasePopWindow {
    private LinearLayout rootLayout;
    private TextView areaView;
    private TextView typeView;
    private TextView nameView;
    private TextView brandView;
    private TextView okView;
    private TextView resetView;
    private MaxRecyclerView areaRecyclerView;
    private MaxRecyclerView typeRecyclerView;
    private MaxRecyclerView nameRecyclerView;
    private MaxRecyclerView brandRecyclerView;
    private MaterialFilterAdapter3 areaAdapter;
    private MaterialFilterAdapter3 typeAdapter;
    private MaterialFilterAdapter3 nameAdapter;
    private MaterialFilterAdapter3 brandAdapter;
    private FilterResultClick filterResultClick;
    private MaterialFilterJson materialFilterJson;
    private List<String> areaList;
    private List<String> materKeyList;
    private List<String> materValueList;
    private List<String> tempMaterKeyList;
    private List<String> tempMaterValueList;
    private List<MaterialFilterAdapter3> adapterList;
    private List<String> b = new ArrayList<>();
    private boolean isFir = true;

    public interface FilterResultClick {
        void result(MaterialFilterJson materialFilterJson, List<DictEntity3> data, SparseBooleanArray checkBooleanArray);

        void queryTypeChild(String id);

        void queryNameChild(String id);
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }

    public MaterialFliterPopWindow(Context context) {
        super(context, DIR_RIGHT);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwin_material_fliter;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        rootLayout = holder.getPop().findViewById(R.id.root_layout);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        areaView = holder.getPop().findViewById(R.id.area_view);
        typeView = holder.getPop().findViewById(R.id.type_view);
        nameView = holder.getPop().findViewById(R.id.name_view);
        brandView = holder.getPop().findViewById(R.id.brand_view);
        areaRecyclerView = holder.getPop().findViewById(R.id.area_list);
        typeRecyclerView = holder.getPop().findViewById(R.id.type_list);
        nameRecyclerView = holder.getPop().findViewById(R.id.name_list);
        brandRecyclerView = holder.getPop().findViewById(R.id.brand_list);
        areaRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        typeRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        nameRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        brandRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        areaAdapter = new MaterialFilterAdapter3(mContext, null);
        areaAdapter.setDefItem(-1);
        areaAdapter.setMultiSelect(true);
        areaAdapter.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<DictEntity3>() {
            @Override
            public void onItemMultiClick(List<DictEntity3> materialFilterEntities, int position) {
                areaList.clear();
                for (DictEntity3 filterEntity : materialFilterEntities) {
                    areaList.add(filterEntity.getId());
                }
//                materialFilterJson.setAreaId(areaList);
            }
        });
        areaRecyclerView.setAdapter(areaAdapter);

        typeAdapter = new MaterialFilterAdapter3(mContext, null);
        typeAdapter.setItemSelect(true);
        typeAdapter.setDefItem(-1);
        typeAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                if (filterResultClick != null) {
                    filterResultClick.queryTypeChild(materialFilterEntity.getId());
                }
                materialFilterJson.setCategoryId(materialFilterEntity.getId());
            }
        });
        typeRecyclerView.setAdapter(typeAdapter);

        nameAdapter = new MaterialFilterAdapter3(mContext, null);
        nameAdapter.setItemSelect(true);
        nameAdapter.setDefItem(-1);
        nameAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                if (filterResultClick != null) {
                    filterResultClick.queryNameChild(materialFilterEntity.getId());
                }
                materialFilterJson.setItemId(materialFilterEntity.getId());

            }
        });
        nameRecyclerView.setAdapter(nameAdapter);
        brandAdapter = new MaterialFilterAdapter3(mContext, null);
        brandAdapter.setItemSelect(true);
        brandAdapter.setDefItem(-1);
        brandAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                b.clear();
                b.add(materialFilterEntity.getId());
                materialFilterJson.setBrandId(b);
            }
        });
        brandRecyclerView.setAdapter(brandAdapter);

        materialFilterJson = new MaterialFilterJson();
        areaList = new ArrayList<>();
        materKeyList = new ArrayList<>();
        materValueList = new ArrayList<>();
        tempMaterKeyList = new ArrayList<>();
        tempMaterValueList = new ArrayList<>();
        adapterList = new ArrayList<>();

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                tempMaterKeyList.clear();
                tempMaterValueList.clear();
                for (int i = 0; i < materValueList.size(); i++) {
                    if (!materValueList.get(i).equals("")) {
                        tempMaterValueList.add(materValueList.get(i));
                        tempMaterKeyList.add(materKeyList.get(i));
                    }
                }
                materialFilterJson.setMaterKey(tempMaterKeyList);
                materialFilterJson.setMaterValue(tempMaterValueList);
                if (filterResultClick != null) {
                    filterResultClick.result(materialFilterJson, areaAdapter.getCheckDatas(), areaAdapter.getCheckList());
                }
            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetData();
            }
        });
    }

    private void resetData() {
        for (MaterialFilterAdapter3 adapter : adapterList) {
            adapter.setDefItem(-1);
        }
        for (int i = 0; i < materValueList.size(); i++) {
            materValueList.set(i, "");
        }
        materialFilterJson.setMaterKey(null);
        materialFilterJson.setMaterValue(null);
        materialFilterJson.setBrandId(new ArrayList<>());
        materialFilterJson.setItemId(null);
        materialFilterJson.setCategoryId(null);
        typeAdapter.setDefItem(-1);
        nameAdapter.setDefItem(-1);
        brandAdapter.setDefItem(-1);
        areaList.clear();
        materialFilterJson.setAreaId(null);
        areaAdapter.resetCheckDatas();
    }

    public void setAreaData(List<DictEntity3> data) {
        if (data.size() == 0) {
            areaView.setVisibility(View.GONE);
        } else {
            areaView.setVisibility(View.VISIBLE);
        }
        areaAdapter.setDatas(data);
        areaAdapter.getCheckDatas();
    }

    public void setAreaDefault(List<DictEntity3> data, SparseBooleanArray checkBooleanArray) {
        areaAdapter.setCheckDefultDatas(data, checkBooleanArray);
        areaList.clear();
        for (DictEntity3 filterEntity : data) {
            areaList.add(filterEntity.getId());
        }
        //       materialFilterJson.setAreaId(areaList);
    }

    public void setTypeData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            typeView.setVisibility(View.GONE);
        } else {
            typeView.setVisibility(View.VISIBLE);
        }
        if (isFir) {
            typeAdapter.setDefItem(0);
            materialFilterJson.setCategoryId(data.get(0).getId());
            isFir = false;
        }
        typeAdapter.setDatas(data);
    }

    public void setTypeDefault(int position, String id) {
        typeAdapter.setDefItem(position);
        materialFilterJson.setCategoryId(id);
    }

    public void setNameData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            nameView.setVisibility(View.GONE);
        } else {
            nameView.setVisibility(View.VISIBLE);
        }
        if (data == null || data.size() == 0) {
            return;
        }
        nameAdapter.setDefItem(0);
        materialFilterJson.setItemId(data.get(0).getId());
        nameAdapter.setDatas(data);
    }

    public void setNameDefault(int position, String id) {
        nameAdapter.setDefItem(position);
        materialFilterJson.setItemId(id);
    }

    public void setBrandData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            brandView.setVisibility(View.GONE);
        } else {
            brandView.setVisibility(View.VISIBLE);
        }
        brandAdapter.setDatas(data);
    }

    public void setBrandDefault(int position, String id) {
        brandAdapter.setDefItem(position);
        b.clear();
        b.add(id);
        materialFilterJson.setBrandId(b);
    }

    public void setSpecData(List<DictEntity3> data) {
        adapterList.clear();
        rootLayout.removeAllViews();
        materKeyList.clear();
        materValueList.clear();
        for (int i = 0; i < data.size(); i++) {
            materKeyList.add(data.get(i).getName());
            materValueList.add(data.get(i).getId());
            addTitleView(data.get(i).getName());
            addRecyclerView(data.get(i).getDatas(), i);
        }
    }

    private void addTitleView(String title) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        TextView textView = new TextView(mContext);
        textView.setText(title);
        textView.setTextSize(16);
        textView.setTextColor(mContext.getResources().getColor(R.color.color_333333));
        rootLayout.addView(textView, layoutParams);
    }

    private void addRecyclerView(List<DictEntity3> data, final int index) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        MaterialFilterAdapter3 childAdapter = new MaterialFilterAdapter3(mContext, null);
        childAdapter.setItemSelect(true);
        childAdapter.setDefItem(-1);
        childAdapter.setIndex(index);
        adapterList.add(childAdapter);
        MaxRecyclerView recyclerView = new MaxRecyclerView(mContext);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(childAdapter);
        childAdapter.setDatas(data);
        childAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                materValueList.set(childAdapter.getIndex(), materialFilterEntity.getId());
            }
        });
        rootLayout.addView(recyclerView, layoutParams);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }
}
